package com.yh.workbill.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.yh.workbill.R;
import com.yh.workbill.databinding.ItemCompanylistinfoBinding;
import com.yh.workbill.model.Company;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

public class CompanyListInfoAdapter extends RecyclerView.Adapter<CompanyListInfoAdapter.ViewHolder> {

    private List<Company> data;
    private OnLongClickLister onLongClickLister;

    public void setOnLongClickLister(OnLongClickLister onLongClickLister) {
        this.onLongClickLister = onLongClickLister;
    }

    public CompanyListInfoAdapter(List<Company> data) {
        this.data = data;
    }

    public void setData(List<Company> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemCompanylistinfoBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_companylistinfo, parent, false);
        return new ViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Company company = data.get(position);
        holder.binding.setData(company);
        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (onLongClickLister != null) {
                    onLongClickLister.onItemClick(position, company);
                }
                return true;
            }
        });
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        ItemCompanylistinfoBinding binding;

        public ViewHolder(@NonNull ItemCompanylistinfoBinding itemView) {
            super(itemView.getRoot());
            this.binding = itemView;
        }
    }

    public interface OnLongClickLister {
        void onItemClick(int position, Company data);
    }
}
